﻿package net.pezzato.genetree.graphics{
	import fl.controls.ProgressBar;
	import flash.display.SpreadMethod;
	import flash.display.Sprite;
	import net.pezzato.graphics.Photo;
	import net.pezzato.graphics.Widget;
	import net.pezzato.genetree.info.GeneNode;
	import net.pezzato.genetree.graphics.Link;
	import net.pezzato.genetree.graphics.Sky;
	import net.pezzato.genetree.graphics.Container;
	import net.pezzato.genetree.graphics.InfoWidget;
	import flash.geom.Rectangle;
	import flash.text.TextFormat;
	import flash.text.TextField;
	import flash.events.MouseEvent;
	import flash.events.Event;
	import flash.filters.GlowFilter;
	import flash.filters.ColorMatrixFilter;
	import flash.display.DisplayObject;
	import flash.geom.Point;
	import net.pezzato.graphics.FullStagePhoto;
	public class NodeWidget extends Widget {
		public static  const COMP_RECT:Rectangle=new Rectangle(0, 0, 320, 32);
		public static  const EXPA_RECT:Rectangle = new Rectangle(0, 0, 320, 240);
		public static  const X_MARGIN:Number = 32;
		public static  const Y_MARGIN:Number = 64;
		private static  const RECTS:Array = new Array(EXPA_RECT, COMP_RECT);
		private static  const GLOW:GlowFilter = new GlowFilter(0xffffff, 0.5, 10, 10);
		protected var titleText_:String;
		protected var mNw_:NodeWidget;
		protected var fNw_:NodeWidget;
		protected var infoWidget_:InfoWidget;
		protected var photo_:Sprite;
		protected static const MALE_COLOR_MATRIX:ColorMatrixFilter =  new ColorMatrixFilter(new Array( //
			0.9, 0, 0, 0, 0, //
			0, 0.9, 0, 0, 0, //
			0, 0, 1, 0, 0, //
			0, 0, 0, 1, 0 //
			));
		protected static const FEMALE_COLOR_MATRIX:ColorMatrixFilter =  new ColorMatrixFilter(new Array( //
			1, 0, 0, 0, 0, //
			0, 0.9, 0, 0, 0, //
			0, 0, 0.9, 0, 0, //
			0, 0, 0, 1, 0 //
			));
		protected var headLink_:Link;
		private var rect_:Rectangle;
		protected var nodeTitle_:String="id";
		protected var title_:TextField;
		protected var colorMatrix_:ColorMatrixFilter;
		private var depth_:Number;
		/*
		 * Status:
		 *  0 = compressed
		 *  1 = expanded
		 *  2 = expandind
		 *  3 = compressing
		 */
		private var status_:Number;
		public function NodeWidget(wp:Widget) {
			super(wp);
			titleText_ = "";
			doubleClickEnabled = true;
			addEventListener(MouseEvent.CLICK, onClick);
			addEventListener(MouseEvent.DOUBLE_CLICK, onDoubleClick);
			addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			status_ = 0;
			rect_ = COMP_RECT;
			drawBg();
			drawTitle();
		}
		public function set nodeTitle(nodeTitle:String):void {
			nodeTitle_= nodeTitle;
			drawTitle();
			if (mNw_ != null) {
				mNw_.nodeTitle = nodeTitle;
			}
			if (fNw_) {
				fNw_.nodeTitle = nodeTitle;
			}
		}
		public function get isExpanded():Boolean {
			return Boolean(1.5 - Math.abs(status_ - 1.5));
		}
		public function get depth():Number { return depth_; }
		public function set depth(value:Number):void {
			depth_ = value;
		}
		public function expand():void {
			toForeground();
			if (status_ < 2) {
				status_ += 2;
				onStatusChanged();
				addEventListener(Event.ENTER_FRAME, onEnterFrame);
			}
		}
		public function get headLink():Link {
			return headLink_;
		}
		protected function drawTitle():void {
			var fo:TextFormat=new TextFormat("Verdana",14,0x333333,true,false,false,null,null,"left");
			if (title_) {
				removeChild(title_);
			}
			title_=new TextField  ;
			title_.defaultTextFormat=fo;
			title_.x = rect_.x + 10;
			title_.y = rect_.y + 5;
			title_.width = rect_.width - 20;
			title_.height = rect_.height - 10;
			title_.selectable = false;
			title_.text = titleText_;
			title_.mouseEnabled = false;
			addChild(title_);
		}
		private function drawLines():void {
			graphics.lineStyle(4,0x666666,0.5,true);
			graphics.moveTo(rect_.x + 8,rect_.y + 32);
			graphics.lineTo(rect_.width - 8,rect_.y + 32);
		}
		private function drawBg():void {
			graphics.lineStyle(4,0x666666,0.5,true);
			graphics.beginFill(0xcccccc,1);
			graphics.drawRoundRect(rect_.x,rect_.y,rect_.width,rect_.height,12,12);
		}
		private function onDoubleClick(event:MouseEvent):void {
			toForeground();
			if (status_ < 2) {
				status_ += 2;
				onStatusChanged();
				addEventListener(Event.ENTER_FRAME, onEnterFrame);
			}
		}
		private function onEnterFrame(event:Event):void {
			if (status_ < 2) {
				removeEventListener(Event.ENTER_FRAME, onEnterFrame);
				return;
			}
			if (Math.abs(rect_.width - RECTS[status_ - 2].width + rect_.height - RECTS[status_ - 2].height) < 1) {
				rect_ = RECTS[status_ - 2];
				removeEventListener(Event.ENTER_FRAME, onEnterFrame);
				status_ = 3 - status_;
				onStatusChanged();
			} else {
				rect_ = new Rectangle(0, 0, (rect_.width + RECTS[status_ - 2].width) / 2,  (rect_.height + RECTS[status_ - 2].height) / 2 );
			}
			redraw();
		}
		private function redraw():void {
			graphics.clear();
			drawBg();
			if (status_ == 1) {
				drawLines();
			}
		}
		private function onMouseOver(event:MouseEvent) {
			this.filters = [colorMatrix_, GLOW];
		}
		private function onMouseOut(event:MouseEvent) {
			this.filters = [colorMatrix_];
		}
		private function onClick(event:MouseEvent) {
			toForeground();
		}
		private function onStatusChanged() {
			switch (status_) {
				case 0: {
					/* compressed */
					break;
				}
				case 1: {
					/* expanded */
					addChild(infoWidget_);
					if (photo_ != null) {
						addChild(photo_);
					}
					infoWidget_.show();
					break;
				}
				case 2: {
					/* expandind */
					break;
				}
				case 3: {
					//* compressing */
					try {
						infoWidget_.hide();
						removeChild(infoWidget_);
						removeChild(photo_);
					} catch (error:Error) {
						// se ci sono errori qui non ci interessa
					}
					break;
				}
			}
		}
	}
}